package cc.telecomdigital.tdstock.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;

public class NewsListItemAdapter extends BaseAdapter
{
	private LayoutInflater		minflater;
	private List<NewsViewHolder>	list;
	private static final String	DATE_REG	= "[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}";

	public NewsListItemAdapter(LayoutInflater inflater,
			List<NewsViewHolder> list)
	{
		super();
		this.minflater = inflater;
		this.list = list;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position < this.list.size() ? this.list.get(position) : null;
	}

	@Override
	public long getItemId(int position)
	{
		return position < this.list.size() ? this.list.get(position).id : -1l;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		NewsViewHolder newsViewHolder = null;
		if (convertView == null)
		{
			convertView = minflater.inflate(R.layout.news_list_item, null);
			newsViewHolder = new NewsViewHolder();
			newsViewHolder.seq = (TextView) convertView
					.findViewById(R.id.news_seq);
			newsViewHolder.text = (TextView) convertView
					.findViewById(R.id.news_title);
			newsViewHolder.sign = (ImageView) convertView
					.findViewById(R.id.news_nextpage);
			newsViewHolder.isPaging = this.list.get(position).isPaging;
			//synchronize with iphone
			newsViewHolder.text.setTextColor(Color.BLACK);
			convertView.setDrawingCacheBackgroundColor(Color.WHITE);

			//synchronize with iphone

			convertView.setTag(newsViewHolder);
		}
		else
		{
			newsViewHolder = (NewsViewHolder) convertView.getTag();
		}
		
		newsViewHolder.text.setText(this.list.get(position).text.getText());

		if (newsViewHolder.isPaging)
		{
			if (position != this.list.size() - 1)
			{
				newsViewHolder.text.setTextColor(Color.BLACK);
				newsViewHolder.seq.setText(String.valueOf(position + 1));
				newsViewHolder.sign
						.setImageResource(R.drawable.go_next_icon);
				//newsViewHolder.text.setTextColor(Color.WHITE);
			}
			else if (!this.list.get(position).text.getText().toString()
					.matches(DATE_REG))
			{
				newsViewHolder.seq.setText("");
				newsViewHolder.sign.setImageDrawable(null);
				newsViewHolder.sign.setBackgroundDrawable(null);
				newsViewHolder.text.setTextColor(0xff5597f9);
			}
		}
		else
		{
			newsViewHolder.seq.setText(String.valueOf(position + 1));
			newsViewHolder.sign.setImageResource(R.drawable.go_next_icon);
		}
		// modify by zor(ouyangzhihui)  --start
		if (getCount() == 1 && position == 0
				&& this.list.get(position).id == -1)
		{
			newsViewHolder.seq.setText("");
			newsViewHolder.text.setTextColor(Color.BLUE);
		}
		else
		{
			newsViewHolder.text.setTextColor(Color.BLACK);
		}
		// modify by zor(ouyangzhihui)  --end

		/*   if(position != this.list.size()-1) {
			   newsViewHolder.seq.setText( String.valueOf(position + 1));
			   newsViewHolder.sign.setImageResource(R.drawable.go_next_icon);
			   //newsViewHolder.text.setTextColor(Color.WHITE);
		   }
		   else if(!this.list.get(position).text.getText().toString().matches(DATE_REG)){
			   newsViewHolder.seq.setText("");
			   newsViewHolder.sign.setImageDrawable(null);
			   newsViewHolder.sign.setBackgroundDrawable(null);
			   newsViewHolder.text.setTextColor(0xff5597f9);
		   }*/

		return convertView;
	}

}
